package com.sunfeng.chap2.p13;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @Author : 孙峰
 * @Description:  验证共享多能自己读
 * @Date : 2021/10/9  16:41
 */
public class Test13_9 {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("d:\\nio\\a.txt", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();

        fileChannel.lock(1, 2, true);
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        fileChannel.read(byteBuffer);
        byteBuffer.flip();

        for (int i = 0; i < byteBuffer.limit(); i++) {
            System.out.print((char) byteBuffer.get());
        }

    }
}
